package is.web.table;

import is.NullUtil;
import is.interfaces.Dao;
import is.interfaces.Table;
import is.model.bean.*;
import is.model.dao.RegionDao;
import is.model.exceptions.EmptyValueException;
import is.web.GetParam;
import java.sql.SQLException;

/**
 *
 * @author aspr0
 */
public class RegionTable extends AbstractHierarchyTable<Region> implements Table<Region>
{
    @Override
    public String genTable() throws EmptyValueException, SQLException
    {

        String table =
                "<table>"
                + "<thead>%s</thead>"
                + "<tbody>%s%s</tbody>"
                + "</table>";
        String header =
                "<tr>"
                + "<td><a href=\".\">id</a> %s</td>"
                + "<td><a href=\"?" + GetParam.ORDER_BY + "=1\">name</a> %s</td>"
                + "<td><a href=\"?" + GetParam.ORDER_BY + "=0\">parent region</a> %s</td>"
                + "<td>tools</td>"
                + "</tr>";
        String id = (hasSearched) ? NullUtil.toString(initialBean.getId()) : "";
        String name = (hasSearched) ? NullUtil.toString(initialBean.getName()) : "";
        String searchForm =
                "<tr>"
                + "<form method=\"post\">"
                + "<td><input type=\"text\" name=\"id\" value=\"" + id + "\" class=\"search-id\"></td>"
                + "<td><input type=\"text\" name=\"name\" value=\"" + name + "\" class=\"search-wide\"></td>"
                + "<td></td>"
                + "<td><input type=\"submit\" value=\"search\"></td>"
                + "</form>"
                + "</tr>";
        String body = "";

        switch (orderBy)
        {
            case 0:
                header = String.format(header, "", "", ARROW_UP + "i", "", "");
                break;

            case 1:
                header = String.format(header, "", ARROW_UP, "", "", "");
                break;

            default:
                String hierarchyBuilt = "";
                if (!hasSearched)
                {
                    buildHierarchy();
                    hierarchyBuilt = "h";
                }
                header = String.format(header, ARROW_UP + hierarchyBuilt, "", "", "", "", "");
                break;
        }

        for (Region region : beans)
        {
            initialBean = new Region();
            initialBean.setId(region.getParentId());
            Dao<Region> regionDao = new RegionDao();
            regionDao.setInitialBean(initialBean);
            Region parentRegion = regionDao.retrieve();
            String parentRegionName = NullUtil.concat(parentRegion.getName());


            String row =
                    "<tr>"
                    + "<td>" + region.getId() + "</td>"
                    + "<td>" + region.getName() + "</td>"
                    + "<td>" + parentRegionName + "</td>"
                    + "<td>"
                    + "<a href=\"retrieve.jsp?id=" + region.getId() + "\" title=\"retrieve\">"
                    + "<image src=\"../img/document-open.png\" class=\"hMargin\"></a>"
                    + "<a href=\"update.jsp?id=" + region.getId() + "\" title=\"update\">"
                    + "<image src=\"../img/document-properties.png\" class=\"hMargin\"></a>"
                    + "<a href=\"copy.jsp?id=" + region.getId() + "\" title=\"copy\">"
                    + "<image src=\"../img/edit-copy.png\" class=\"hMargin\"></a>"
                    + "<a href=\"delete.jsp?id=" + region.getId() + "\" title=\"delete\">"
                    + "<image src=\"../img/edit-delete.png\" class=\"hMargin\"></a>"
                    + "</td>"
                    + "</tr>";
            body += row;
        }
        return String.format(table, header, searchForm, body);
    }
}
